package jp.tuyano.helo.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

public class Add implements EntryPoint {

    private Label label;
    private TextBox name;
    private TextBox mail;
    private TextBox tel;
    private TextArea memo;
    
    private final AddPDServiceAsync addService = GWT.create(AddPDService.class);
    
    private static final String SERVER_ERROR = "SERVER_ERROR!!";
    
    public void onModuleLoad() {
        label = new Label("please type new PersonalData.");
        RootPanel.get("add_label").add(label);
        FlexTable panel = new FlexTable();
        name = new TextBox();
        panel.setText(0, 0, "Name");
        panel.setWidget(0, 1, name);
        mail = new TextBox();
        panel.setText(1, 0, "Mali");
        panel.setWidget(1, 1, mail);
        tel = new TextBox();
        panel.setText(2, 0, "Tel");
        panel.setWidget(2, 1, tel);
        memo =new TextArea();
        memo.setSize("200px", "100px");
        panel.setText(3, 0, "Memo");
        panel.setWidget(3, 1, memo);
        Button button = new Button("Click");
        panel.setWidget(4, 1, button);
        button.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                addService.addEntities(name.getText(), mail.getText(),
                        tel.getText(), memo.getText(),
                        new AsyncCallback<String>() {
                            public void onSuccess(String result) {
                                label.setText(result);
                            }
                            public void onFailure(Throwable caught) {
                                
                                label.setText(SERVER_ERROR);
                            }
                        });
            }
        });
        RootPanel.get("add_panel").add(panel);
    }
}
